package org.marverse.scripting

import ScriptingUtil._

sealed trait UncheckedLiteralExpr extends UncheckedExpression {
  val value: Any
  val valueType: Type
  // Literals cannot be malformed
  override def checked(implicit ctx: MutableTypedMap[TypedKey, Any]): LiteralExpr
}

object UncheckedLiteralExpr {
  def apply(value: Any, valueType: Type): UncheckedLiteralExpr =
    new LiteralExprImpl(value, valueType)
  def unapply(expr: UncheckedLiteralExpr) = Some(expr.value, expr.valueType)
}

sealed abstract case class LiteralExpr(val value: Any, val valueType: Type) extends Expression {

}

private class LiteralExprImpl(value: Any, valueType: Type) 
extends LiteralExpr(value, valueType) with UncheckedLiteralExpr {
  assert(valueType.isInstance(value))
  
  override def check(implicit ctx: MutableTypedMap[TypedKey, Any]) {
    super.check
    assertThat(valueType.isInstance(value),
      "LiteralExpr: valueType (%s) isInstance value (%s)".format(valueType, value))
  }
  
  override def childExpressions = Seq.empty
  override def childUncheckedExpressions = Seq.empty
  
  override def toString = value.toString
  override def checked(implicit ctx: MutableTypedMap[TypedKey, Any]) = this
  override def expressionType(implicit ctx: MutableTypedMap[TypedKey, Any]) = valueType
  override def evaluate(implicit ctx: MutableTypedMap[TypedKey, Any]) = value

}
